﻿using System;
using System.Collections.Generic;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;

namespace Model {
  
  public class Bairro {

    private string idBairro;
    private String nome;

    public string IdBairro {
      get { return idBairro; }
	  set { idBairro = value; }
    }

    public string Nome {
      get { return nome; }
      set { nome = value; }
    }

    #region Metodos

    #region Persist
    public Boolean Persist()
    {
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objSession.Save(this);
            objTransaction.Commit();
            objSession.Close();
            return true;
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
    }
    #endregion

    #region Update
    public Boolean Update()
    {
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objSession.Update(this);
            objTransaction.Commit();
            objSession.Close();
            return true;
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
    }
    #endregion

    #region Delete
    public static Boolean Delete(int pIdBairro)
    {
        Bairro objBairro = null;
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objBairro = (Bairro)objSession.Load(typeof(Model.Bairro), pIdBairro);
            objSession.Delete(objBairro);
            objTransaction.Commit();
            objSession.Close();
            return true;
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
    }
    #endregion

    #region RetriveObject
    public static Bairro RetriveObject(string pidBairro)
    {
        Bairro objBairro = null;
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objBairro = (Bairro)objSession.Load(typeof(Model.Bairro), pidBairro);
            objSession.Close();
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
        return objBairro;
    }
    #endregion

    #region RetriveObjects
    public static IList RetriveObjects()
    {
        IList listBairro;
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            listBairro = objSession.CreateCriteria(typeof(Model.Bairro)).List();
            objSession.Close();

        }
        catch (Exception ex)
        {
            listBairro = null;
            objTransaction.Rollback();
            throw ex;
        }
        return listBairro;
    }
    #endregion

    #endregion
  }
}
